package org.jsola.contract.helper;

import org.jsola.common.SafeKit;
import org.jsola.contract.dto.CategoryVariableAddDTO;
import org.jsola.contract.dto.CategoryVariableUpdateDTO;
import org.jsola.contract.entity.CategoryVariableDO;
import org.jsola.contract.query.CategoryVariableQuery;
import org.jsola.contract.service.ICategoryVariableService;
import org.jsola.contract.vo.CategoryVariableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 合同分类下模板变量
 *
 * @author JanusMix
 */
@Component
public class CategoryVariableTestHelper extends BaseTestHelper {
    
    @Autowired
    private ICategoryVariableService categoryVariableService;
    @Autowired
    private VariableTestHelper variableTestHelper;

    /**
     * 生成add测试数据
     * @return CategoryVariableAddDTO
     */
    public CategoryVariableAddDTO getCategoryVariableAddDTO(){
        CategoryVariableAddDTO categoryVariableAddDTO = new CategoryVariableAddDTO();
        categoryVariableAddDTO.setCategoryId(SafeKit.getLong(getRandomValue("bigint",11)));
        categoryVariableAddDTO.setVariableId(variableTestHelper.saveVariableExample().getId());
        return categoryVariableAddDTO;
    }


    /**
     * 生成update测试数据
     * @return CategoryVariableUpdateDTO
     */
    public CategoryVariableUpdateDTO getCategoryVariableUpdateDTO(CategoryVariableVO categoryVariableVO){
        return categoryVariableService.selectDOById(categoryVariableVO.getId(), SITE_ID).to(CategoryVariableUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return CategoryVariableQuery
     */
    public CategoryVariableQuery getCategoryVariableQuery() {
        CategoryVariableQuery categoryVariableQuery = new CategoryVariableQuery();
        List<CategoryVariableDO> categoryVariableDOList = categoryVariableService.list(new CategoryVariableDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(categoryVariableDOList)) {
            CategoryVariableDO categoryVariableDO = categoryVariableDOList.get(0);
            categoryVariableQuery = categoryVariableDO.to(CategoryVariableQuery.class);
        }
        return categoryVariableQuery;
    }

    /**
     * 保存示例
     * @return CategoryVariableVO
     */
    public CategoryVariableVO saveCategoryVariableExample(){
        CategoryVariableAddDTO addDTO = this.getCategoryVariableAddDTO();
        CategoryVariableVO categoryVariableVO = categoryVariableService.save(addDTO,buildTokenUser());
        return categoryVariableVO;
    }


}

